<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>
<!-- 1. 购物车 -->

<body>
    <div id="app">
        <p><input type="checkbox" v-model="isAllCheck" />全选</p>
        <ul>
            <li v-for="item in list" :key="item.id">
                <input type="checkbox" v-model="item.checked">
                {{item.name}} - 价格：{{item.price}} 数量：<input type="number" v-model.number="item.quantity">
            </li>
        </ul>
        <p>总价：{{index}}</p>
    </div>
</body>
<script>
    const { createApp, ref, computed } = Vue;
    const app = createApp({
        setup() {
            const list = ref([
                { id: 1, name: ' 苹果', price: 3, quantity: 1, checked: false },
                { id: 2, name: ' 橘子', price: 5, quantity: 2, checked: false },
                { id: 3, name: '葡萄', price: 15, quantity: 3, checked: false }
            ]);
            const isAllCheck = computed({
                get() {
                    return list.value.every(v => v.checked);
                },
                set(val) {
                    list.value.forEach(item => {
                        item.checked = val;
                    });
                }
            });
            const index = computed(() => {
                let total = 0;
                list.value.forEach(item => {
                    if (item.checked) {
                        total += item.price * item.quantity;
                    }
                })
                return total;
            })
            return {
                list,
                index,
                isAllCheck

            }
        }
    }).mount('#app')
</script>

</html>